package com.zcf.airobot.controller;

import com.zcf.airobot.service.ChatHistoryRepository;
import com.zcf.airobot.vo.MessageVo;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zcf
 */
@RestController
@RequestMapping("/ai/history")
@RequiredArgsConstructor
public class ChatHistoryController {


    private final ChatHistoryRepository chatHistoryRepository;

    private final ChatMemory chatMemory;
    /**
     * 获取会话列表
     * @param type 业务类型
     */
    @GetMapping("/{type}")
    public List<String> chatIds(@PathVariable String type) {
        return chatHistoryRepository.getChatIds(type);
    }


    /**
     * 查询指定会话的历史
     */
    @GetMapping("/{type}/{chatId}")
    public List<MessageVo> history(@PathVariable String type, @PathVariable String chatId) {

        List<Message> messages = chatMemory.get(chatId, Integer.MAX_VALUE);
        //内容格式处理
        if(messages==null){
            return List.of();
        }
      return messages.stream().map(message -> new MessageVo(message)).collect(Collectors.toList());
    }
}
